package src.servletFilter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import src.servletUtils.MyUtils;



public class EncodingCharsetFilter implements Filter {
	//定义全局变量
	FilterConfig myconf;
	
	


	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding(myconf.getInitParameter("charSet"));
		response.setCharacterEncoding(myconf.getInitParameter("charSet"));
		// 2、将该请求转发给下一个过滤器或目标servlet
				// （如果没有将请求对象向下转发，则该请求过程会暂停在当前过滤器中）
				// chain 参数描述的是请求转发处理链对象
		//必须写，为了链接，跳转，  转发处理链对象
		MyUtils.logger_console.debug("设置当前编码为"+myconf.getInitParameter("charSet"));
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig conf) throws ServletException {
		//初始化
		//给全局变量复制
		myconf=conf;
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
}
